package com.tnovoselec.android.travelassistant.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.tnovoselec.android.favorites.db.FavoritesDatabaseHelper;
import com.tnovoselec.android.travelassistant.Config;
import com.tnovoselec.android.travelassistant.dto.BankomatDto;
import com.tnovoselec.android.travelassistant.dto.BenzinskaDto;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;

public class TravelAssistantDatabaseHelper extends FavoritesDatabaseHelper {

	private static String DATABASE_NAME = "travelAssistant.db";
	public static int DATABASE_VERSION = 4;

	private Dao<IGeoPoint, String> bankomatDao = null;
	private Dao<IGeoPoint, String> benzinskaDao = null;
	private Context mContext;

	public TravelAssistantDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.mContext = context;
	}

	@Override
	public String getDatabaseName() {
		return DATABASE_NAME;
	}

	@Override
	public int getDatabaseVersion() {
		return DATABASE_VERSION;
	}

	@Override
	public List<String> getCreateQueries() {
		List<String> queries = new ArrayList<String>();
		queries.add(BankomatDto.getCreateQuery());
		queries.add(BenzinskaDto.getCreateQuery());
		return queries;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Class> getDtoClasses() {
		List<Class> classes = new ArrayList<Class>();
		classes.add(BankomatDto.class);
		classes.add(BenzinskaDto.class);
		return classes;
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource1) {
		super.onCreate(database, connectionSource1);
		BufferedReader reader = null;
		String line = null;
		try {
			InputStream is = mContext.getAssets().open(Config.DATA_SCRIPT);
			reader = new BufferedReader(new InputStreamReader(is));

			// String line = null;
			while ((line = reader.readLine()) != null) {
				database.execSQL(line);
				Log.i("Inserting", line);
			}
		} catch (Exception e) {
			Log.e(TravelAssistantDatabaseHelper.class.getSimpleName(), e.getMessage());
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.e(TravelAssistantDatabaseHelper.class.getSimpleName(), e.getMessage());
				}
			}
		}
	}

	public Dao<IGeoPoint, String> getBankomatDao() throws SQLException {
		if (bankomatDao == null) {
			bankomatDao = super.getDao(BankomatDto.class);
		}

		return bankomatDao;
	}

	public Dao<IGeoPoint, String> getBenzinskaDao() throws SQLException {
		if (benzinskaDao == null) {
			benzinskaDao = super.getDao(BenzinskaDto.class);
		}

		return benzinskaDao;
	}

}
